package com.durian.base.utils;

import android.text.TextUtils;
import android.util.Log;

import com.durian.BaseApp;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 说明：LogUtils
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2019-06-14 18:28
 * <p/>
 * 版本：version 1.0
 */
public class LogUtils {

    private static final String Tag = "Durian";
    private static final String LINE_SEPARATOR;

    static {
        LINE_SEPARATOR = System.getProperty("line.separator");
    }

    public static void v(String tag, String message, Object... args) {
        if (BaseApp.isAppDebug()) {
            if (args != null) {
                Log.v(tag, String.format(message, args));
            } else {
                Log.v(tag, message);
            }
        }
    }

    public static void v(String message, Object... args) {
        v(Tag, message, args);
    }

    public static void d(String tag, String message, Object... args) {
        if (BaseApp.isAppDebug()) {
            if (args != null) {
                Log.d(tag, String.format(message, args));
            } else {
                Log.d(tag, message);
            }
        }
    }

    public static void d(String message, Object... args) {
        d(Tag, message, args);
    }

    public static void i(String tag, String message, Object... args) {
        if (BaseApp.isAppDebug()) {
            if (args != null) {
                Log.i(tag, String.format(message, args));
            } else {
                Log.i(tag, message);
            }
        }
    }

    public static void i(String message, Object... args) {
        i(Tag, message, args);
    }

    public static void w(String tag, String message, Object... args) {
        if (BaseApp.isAppDebug()) {
            if (args != null) {
                Log.w(tag, String.format(message, args));
            } else {
                Log.w(tag, message);
            }
        }
    }

    public static void w(String message, Object... args) {
        w(Tag, message, args);
    }

    public static void e(String tag, String message, Object... args) {
        if (BaseApp.isAppDebug()) {
            if (args != null) {
                Log.e(tag, String.format(message, args));
            } else {
                Log.e(tag, message);
            }
        }
    }

    public static void e(String message, Object... args) {
        e(Tag, message, args);
    }

    public static void e(Throwable e) {
        e(Tag, ToolUtils.collectErrorInfo(e));
    }

    public static void json(String content){
        json(Tag,content);
    }

    public static void json(String tag,String content) {
        if (TextUtils.isEmpty(content)) {
            Log.wtf(tag, "Empty or Null json content");
            return;
        }

        String message = null;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            if (content.startsWith("{")) {
                JSONObject jsonObject = new JSONObject(content);
                message = jsonObject.toString(4);
            } else if (content.startsWith("[")) {
                JSONArray jsonArray = new JSONArray(content);
                message = jsonArray.toString(4);
            }
        } catch (JSONException var19) {
            e(tag, var19.getCause().getMessage() + "\n" + content);
            return;
        }

        printLine(tag, true);
        String logStr = stringBuilder.toString();
        message = logStr + LINE_SEPARATOR + message;
        String[] lines = message.split(LINE_SEPARATOR);
        StringBuilder jsonContent = new StringBuilder();
        String[] var15 = lines;
        int var16 = lines.length;

        for (int var17 = 0; var17 < var16; ++var17) {
            String line = var15[var17];
            jsonContent.append("║ ").append(line).append(LINE_SEPARATOR);
        }

        Log.wtf(tag, jsonContent.toString());
        printLine(tag, false);
    }

    private static void printLine(String tag, boolean isTop) {
        if (isTop) {
            Log.wtf(tag, "╔════════════════════════════════════");
        } else {
            Log.wtf(tag, "╚════════════════════════════════════");
        }

    }
}
